package com.tem.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.tem.common.tool.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import com.tem.system.entity.SysParameter;
import com.api.manage.pojo.system.SysParameterBean;
import com.tem.system.service.ISysParameterService;
import com.tem.common.core.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 参数配置 前端控制器
 * </p>
 * @author 腾木
 * @since 2023-11-14
 */
@RestController
@RequestMapping("/system/sysParameter")
public class SysParameterController extends BaseController{
    @Autowired
    private ISysParameterService sysParameterService;
    @ApiOperation(value = "参数配置列表")
    @RequestMapping("list")
    public ResultUtil<IPage<SysParameterBean>> list(SysParameterBean request, HttpServletRequest req){
        QueryWrapper<SysParameter> wrapper = new QueryWrapper<>();
        if(request.getSysBranchOfficeId()==null){
            request.setSysBranchOfficeId(this.getSysBranchOfficeId(req));
        }
        wrapper.eq("system_level_id",request.getSystemLevelId()).eq("sys_branch_office_id",request.getSysBranchOfficeId());
        if(request.getNumb()!=null && request.getNumb().trim().length()>0){
            wrapper.eq("numb",request.getNumb().trim());
        }
        if(request.getParameterName()!=null && request.getParameterName().trim().length()>0){
            wrapper.like("parameter_name",request.getParameterName().trim());
        }
        if(request.getDataType()!=null){
            wrapper.eq("data_type",request.getDataType());
        }
        wrapper.orderByDesc("seq");
        Page page = new Page<>(request.getCurrent(),request.getSize());
        IPage listBody = sysParameterService.page(page,wrapper);
        List<SysParameter> entityList=listBody.getRecords();
        if(!entityList.isEmpty()) {
            List<SysParameterBean> responseList = new ArrayList<>();
            entityList.forEach(it -> {
                SysParameterBean bean = new SysParameterBean();
                BeanUtils.copyProperties(it,bean);
                responseList.add(bean);
            });
            listBody.setRecords(responseList);
        }
        return ResultUtil.success(listBody);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增参数配置")
    public ResultUtil save(@RequestBody SysParameter entity, HttpServletRequest req){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getSysParameterId()!=null){
            sysParameterService.updateById(entity);
        }else{
            entity.setSysBranchOfficeId(this.getSysBranchOfficeId(req));
            entity.setDeleted(0);
            sysParameterService.save(entity);
        }
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询参数配置")
    public ResultUtil<SysParameterBean> getById(@PathVariable String id){
        SysParameter entity = sysParameterService.getById(id);
        if(entity!=null){
            SysParameterBean bean = new SysParameterBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改参数配置")
    public ResultUtil update(@RequestBody SysParameter entity){
        sysParameterService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除参数配置")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        sysParameterService.removeById(id);
        return ResultUtil.success();
    }
}